<template>
  <div style="height: 65px">
    <TopMenu :disabled-count="false" :disabled-trade="false" :disabled-message="false"></TopMenu>
  </div>
  <div>
    <div class="background_div">用户管理</div>
  </div>
  <div>
    <el-form :inline="true" :model="queryParams" style="margin-top: 20px; height: 33px">
      <el-form-item label="用户名称：" style="margin-left: 130px; font-weight: bold">
        <el-input v-model="queryParams.userName" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="用户类型：" style="margin-left: 170px; font-weight: bold">
        <el-input v-model="queryParams.userRole" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-main>
      <el-scrollbar>
        <el-table :data="tableData" stripe style="width: 100%" max-height="300">
          <el-table-column fixed type="index" label="序号" width="100" align="center" />
          <el-table-column prop="userAccount" label="用户名称" width="300" align="center" />
          <el-table-column prop="userRole" label="用户类型" width="300" align="center" />
          <el-table-column label="操作" align="center">
            <template #default="scope">
              <el-button size="small" type="success" @click="handleSuccess(scope.row)"
                >修改
              </el-button>
              <el-button size="small" type="danger" @click="handleDanger(scope.row)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-scrollbar>
    </el-main>
  </div>
  <div>
    <el-pagination
      background
      layout="prev, pager, next"
      :page-count="totalPage"
      @change="doPageChange"
    />
  </div>
  <el-dialog v-model="dialogVisible" title="用户信息修改" width="500">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      status-icon
      label-width="auto"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="pass">
        <el-input v-model="ruleForm.userAccount" autocomplete="off" />
      </el-form-item>
      <el-form-item label="用户权限" prop="checkPass">
        <el-select v-model="ruleForm.UserRole" placeholder="Select" size="large">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm">修改</el-button>
        <el-button @click="resetForm">返回</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup lang="ts">
import { inject, onMounted, ref } from 'vue'
import TopMenu from '@/components/Menu/TopMenu.vue'
import { UserControllerService } from '../../../../generated'
import { ElMessage } from 'element-plus'

const reload = inject('reload')
const dialogVisible = ref(false)
const ruleForm = ref({
  id: '',
  userAccount: '',
  UserRole: ''
})
const totalPage = ref(10)

const tableData = ref()

const options = [
  {
    value: 'admin',
    label: '超级管理员'
  },
  {
    value: 'DataAuditor',
    label: '数据审核员'
  },
  {
    value: 'agency',
    label: '第三方监管机构'
  },
  {
    value: 'user',
    label: '用户'
  },
  {
    value: 'company',
    label: '企业用户'
  }
]
// 查询参数
const queryParams = ref({
  userName: '',
  userRole: '',
  current: 0,
  pageSize: 10
})
const loadData = async () => {
  const res = await UserControllerService.listUserVoByPageUsingPost(queryParams.value)
  tableData.value = res.data.records
  console.log(res)
  totalPage.value = Math.floor(
    (Number(queryParams.value.pageSize - 1) + Number(res.data.total)) /
      Number(queryParams.value.pageSize)
  )
}
onMounted(() => {
  loadData()
})
/**
 * 分页请求
 * @param page
 */
const doPageChange = (page: any) => {
  queryParams.value = page
  loadData()
}
/**
 * 表单 修改
 */
const submitForm = async () => {
  dialogVisible.value = false
  const data = {
    id: ruleForm.value.id,
    userAccount: ruleForm.value.userAccount,
    userRole: ruleForm.value.UserRole
  }
  const res = await UserControllerService.updateUserUsingPost(data)
  if (res.code === 0) {
    ElMessage({
      message: '用户信息修改成功',
      type: 'success'
    })
  } else {
    ElMessage({
      message: '用户信息修改失败',
      type: 'error'
    })
  }
  reload()
}
/**
 * 表单 返回
 */
const resetForm = () => {
  dialogVisible.value = false
}

function onSubmit() {}

/**
 * 修改
 * @param index
 */
const handleSuccess = async (index: any) => {
  dialogVisible.value = true
  console.log(index)
  ruleForm.value.userAccount = index.userAccount
  ruleForm.value.UserRole = index.userRole
  ruleForm.value.id = index.id
}

/**
 * 删除
 * @param index
 */
const handleDanger = async (index: any) => {
  const res = await UserControllerService.deleteUserUsingPost({ id: index.id })
  if (res.code === 0) {
    ElMessage({
      message: '用户信息删除成功',
      type: 'success'
    })
  } else {
    ElMessage({
      message: '用户信息删除失败',
      type: 'error'
    })
  }
}
</script>

<style scoped>
.background_div {
  background-color: #409eff;
  padding: 10px;
  margin: 10px;
}

.pagination-style {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
